package com.mocha.util.excel;

import com.mocha.util.EmptyUtil;
import com.mocha.util.json.JsonTypeReferences;
import com.mocha.util.json.JsonUtil;
import org.jxls.common.Context;
import org.jxls.expression.JexlExpressionEvaluator;
import org.jxls.transform.Transformer;
import org.jxls.util.JxlsHelper;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

public class ExcelUtil {

    /**
     *
     * @param inputStream
     *            jxls2模板文件
     * @param outputStream
     *            输入文件
     * @param funcs
     *            自定义函数
     * @param values
     */
    public static void write(InputStream inputStream, OutputStream outputStream, Map<String, Object> values,
                             Map<String, Object> funcs) throws Exception {
        Context context = map2JxlsContext(values);
        JxlsHelper jxlsHelper = JxlsHelper.getInstance();
        Transformer transformer = jxlsHelper.createTransformer(inputStream, outputStream);
        if (!EmptyUtil.isEmpty(funcs)) {
            JexlExpressionEvaluator evaluator = (JexlExpressionEvaluator) transformer.getTransformationConfig()
                    .getExpressionEvaluator();
            evaluator.getJexlEngine().setFunctions(funcs);
        }
        jxlsHelper.processTemplate(context, transformer);
    }

    /**
     * @param templatePath
     *            classpath下的模板路径
     * @param outputStream
     * @param values
     * @throws Exception
     */
    public static void write(String templatePath, OutputStream outputStream, Map<String, Object> values,
                             Map<String, Object> funcs) throws Exception {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(templatePath);
        write(inputStream, outputStream, values, funcs);
    }

    public static void write(InputStream inputStream, OutputStream outputStream, Map<String, Object> values)
            throws Exception {
        write(inputStream, outputStream, values, null);
    }

    public static void write(String templatePath, OutputStream outputStream, Map<String, Object> values)
            throws Exception {
        write(templatePath, outputStream, values, null);
    }

    public static Context map2JxlsContext(Map<String, Object> values) {
        Context context = new Context();
        if (!EmptyUtil.isEmpty(values)) {
            for (Map.Entry<String, Object> entry : values.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                if (value != null && key != null) {
                    context.putVar(key, value);
                }
            }
        }
        return context;
    }



    /**
     * 自定义函数
     * @author zhuhui
     * 后面又需要添加自定义的可以在此添加功能
     */
    public static class Function{
        public static String get(String json,String key) {
            if(EmptyUtil.isEmpty(json)) {
                return null;
            }
            Map<String, Object> tmp=JsonUtil.readValue(json, JsonTypeReferences.REF_MAP_OBJECT);
            Object value=tmp.get(key);
            if(value==null) {
                return "";
            }
            return (String)value;
        }
    }

}
